package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface OrderMapper {
    void insert(Orders orders);
    @Update("update orders set checkout_time = #{now},pay_method=#{ordersPaymentDTO.payMethod},status = 2 where number = #{ordersPaymentDTO.orderNumber}")
    int updateCheckOutTime(OrdersPaymentDTO ordersPaymentDTO, LocalDateTime now);

    Page<OrderVO> query(OrdersPageQueryDTO pageQueryDTO);
    @Select("        select count(if(status = 2 , true , null)) as toBeConfirmed,\n" +
            "               count(if(status = 3 , true , null)) as confirmed,\n" +
            "               count(if(status = 4 , true , null)) as deliveryInProgress\n" +
            "               from orders")
    OrderStatisticsVO statistics();
    @Update("update orders set status = #{i} where id = #{id}")
    void updateStatusById(Long id, int i);
    @Update("update orders set status = 5 , delivery_time = now() where id=#{id}")
    void completeById(Long id);
    @Update("update orders set status = 6 , cancel_reason = #{cancelReason} , cancel_time = now() where id = #{id}")
    void cancel(OrdersCancelDTO dto);
    @Select("select * from orders where id =#{id}")
    OrderVO getById(Long id);
    @Select("select * from orders where status = #{status} and order_time < #{localDateTime}")
    List<Orders> getByStatusAndTime(Integer status, LocalDateTime localDateTime);


    void update(Orders orders);
    @Select("select id from orders where number=#{orderNumber}")
    Long getIdByNumber(String orderNumber);
}
